<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()" ref="dataForm">
      <el-form-item label="人工审核：">
        <el-select v-model="dataForm.reviewState" placeholder="请选择人工审核">
          <el-option
            v-for="item in useroption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="机审状态：">
        <el-select v-model="dataForm.machineReviewState" placeholder="请选择机审状态">
          <el-option
            v-for="item in useroption3"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="笔记权限：">
        <el-select v-model="dataForm.isPublic" placeholder="请选择笔记权限">
          <el-option
            v-for="item in useroption1"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="查重检测：">
        <el-select v-model="dataForm.repetitionStatus" placeholder="请选择">
          <el-option
            v-for="item in useroption2"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.id" placeholder="笔记编号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.bookId" placeholder="书籍编号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.bookName" placeholder="书籍名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.userId" placeholder="用户uid"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.nickname" placeholder="用户昵称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;"
    >
      <el-table-column prop="id" header-align="center" align="center" label="笔记编号"></el-table-column>
      <el-table-column prop="bookId" header-align="center" align="center" label="书籍编号"></el-table-column>
      <el-table-column prop="bookName" header-align="center" align="center" label="书籍名称">
        <template slot-scope="scope">
          {{ scope.row.bookId === '0'?scope.row.chapterName:scope.row.bookName }}
        </template>
      </el-table-column>
      <el-table-column prop="userId" header-align="center" align="center" label="用户uid"></el-table-column>
      <el-table-column prop="nickName" header-align="center" align="center" label="用户昵称"></el-table-column>
      <el-table-column prop="gmtCreated" header-align="center" align="center" label="发布时间"></el-table-column>
      <el-table-column header-align="center" align="center" label="笔记权限">
        <template slot-scope="scope">
          {{ scope.row.isPublic === 1?'公开':'' }}
          {{ scope.row.isPublic === 0?'私密':'' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="笔记内容" width="200px">
        <template slot-scope="scope">
          <div @mouseover="showNote(scope.row.noteContent)" class="text-el"
               v-html="noteHandleList(scope.row.noteContent)"></div>
        </template>
      </el-table-column>
      <el-table-column prop="userLikeCount" header-align="center" align="center" label="用户点赞"></el-table-column>
      <el-table-column header-align="center" align="center" label="查重">
        <template slot-scope="scope">
          {{ scope.row.repetitionStatus === 1?'通过':'' }}
          {{ scope.row.repetitionStatus === 2?'不通过':'' }}
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="center" label="机审状态">
        <template slot-scope="scope">
          {{ scope.row.machineReviewState === 1?'通过':'' }}
          {{ scope.row.machineReviewState ===-1?'不通过':'' }}
          {{ scope.row.machineReviewState === 2?'审核失败':'' }}
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="center" label="人工审核状态">
        <template slot-scope="scope">
          {{ scope.row.reviewState === 1?'通过':'' }}
          {{ scope.row.reviewState ===-1?'不通过':'' }}
          {{ scope.row.reviewState === 0?'—':'' }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" label="操作" width="220px">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.reviewState === -1 || (scope.row.reviewState === 0 && scope.row.machineReviewState !== 1)"
            type="text" size="small" @click="dataFormSubmit(scope.row, 1)">通过
          </el-button>
          <el-button
            v-if="scope.row.reviewState === 1 || (scope.row.reviewState === 0 && scope.row.machineReviewState !== -1)"
            style="color: red;" type="text" size="small" @click="dataFormSubmit(scope.row, -1)">不通过
          </el-button>
          <el-button type="text" size="small" @click="lookOrCheck(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

    <!--笔记预览-->
    <div v-if="shownote" class="preview" @mouseleave="hideNote">
      <div v-html="noteHtml"></div>
    </div>

    <!-- 弹窗, 新增 / 修改 -->
    <look-or-check ref="lookOrCheck" @refreshDataList="getDataList"></look-or-check>
  </div>
</template>
<style>
  .disable {
    pointer-events: none;
    color: #ccc;
    text-decoration: underline;
    margin-right: 5px;
  }

  .disable .el-button--text {
    color: #ccc;
  }

  .active {
    cursor: pointer;
  }

  .text-el {
    cursor: pointer;
    width: 180px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .preview {
    position: fixed;
    padding: 20px;
    width: 42%;
    height: 70%;
    left: 20%;
    top: 150px;
    z-index: 99999;
    background: #fff;
    border: 1px solid #000000;
    border-radius: 4px;
    overflow: auto;
    line-height: 22px;
    font-size: 15px;
  }
</style>

<script>
  import LookOrCheck from './lookOrCheck'

  export default {
    data() {
      return {
        shownote: false,
        dataForm1: {},
        dataForm2: {},
        zanConfig: 0,
        visible1: false,
        noteHtml: '123',
        useroption: [
          {
            value: null,
            label: '全部'
          },
          {
            value: '0',
            label: '未审核'
          },
          {
            value: '1',
            label: '通过'
          },
          {
            value: '-1',
            label: '不通过'
          }
        ],
        useroption1: [
          {
            value: null,
            label: '全部'
          },
          {
            value: '1',
            label: '公开'
          },
          {
            value: '0',
            label: '私密'
          }
        ],
        useroption2: [
          {
            value: 0,
            label: '全部'
          },
          {
            value: 1,
            label: '通过'
          },
          {
            value: 2,
            label: '不通过'
          }
        ],
        useroption3: [
          {
            value: null,
            label: '全部'
          },
          {
            value: 1,
            label: '通过'
          },
          {
            value: -1,
            label: '不通过'
          },
          {
            value: 2,
            label: '审核失败'
          }
        ],
        dataForm: {
          reviewState: null,
          machineReviewState: null,
          isPublic: '1',
          repetitionStatus: 1
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false
      }
    },
    components: {
      LookOrCheck
    },
    activated() {
      this.getDataList()
    },
    methods: {
      search() {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.dataForm.page = this.pageIndex
        this.dataForm.limit = this.pageSize
        this.$http({
          url: this.$http.adornUrl('/course/noterecord/list'),
          method: 'get',
          params: this.$http.adornParams(this.dataForm)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$refs['dataForm'].resetFields()
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      showNote(note) {
        this.shownote = true
        this.noteHtml = this.noteHandle(note)
      },
      hideNote() {
        this.shownote = false
      },
      noteHandleList(note) {
        if (note) {
          let noteHtml = ''
          let noteArr = note
          noteArr.map((item) => {
            if (item.type === 'text') {
              noteHtml += item.content
            } else if (item.type === 'image') {
              // noteHtml += `<img src="${item.content}" style="width: 100%;margin: 15px 0;"` + '<br>'
            }
          })
          return noteHtml
        }
      },
      noteHandle(note) {
        if (note) {
          let noteHtml = ''
          let noteArr = note
          noteArr.map((item) => {
            if (item.type === 'text') {
              noteHtml += item.content + '<br>'
            } else if (item.type === 'image') {
              noteHtml += `<img src="${item.content}" style="width: 300px;margin: 15px 0;">` + '<br>'
            }
          })
          return noteHtml
        }
      },
      dataFormSubmit(form, result) {
        const that = this
        this.dataForm2 = form
        this.dataForm2.reviewState = result
        this.$http({
          url: this.$http.adornUrl(
            `/course/noterecord/update`
          ),
          method: 'post',
          data: this.$http.adornData(this.dataForm2)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 500,
            });
            // that.getDataList()
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      lookOrCheck(id, type) {
        this.shownote = false
        this.$nextTick(() => {
          this.$refs.lookOrCheck.init(id, type)
        })
      }
    }
  }
</script>
